package selection;


import util.GAConfig;
import util.Randomness;
import ga.Individual;
public class TournamentSelection extends ParentSelector{
	private static final int POPULATION_SIZE = GAConfig.PopulationSize;

	@Override
	public Individual selectTwoParents() {
		
			 // Pick two candidates at random.
			int pos1 = Randomness.nextRandomInt(POPULATION_SIZE);
			int pos2 = Randomness.nextRandomInt(POPULATION_SIZE);
		    while ((pos1 == pos2) && (POPULATION_SIZE>1)) {
		        pos2 = Randomness.nextRandomInt(POPULATION_SIZE);
		      }

			float candidate1Fitness= super.gaInfo.getPopulation()[pos1].getFitness();
			float candidate2Fitness= super.gaInfo.getPopulation()[pos2].getFitness();
			//gaInfo.getPopulation();
            // Select the fitter candidate.
			if(candidate1Fitness>candidate2Fitness)
				return super.gaInfo.getPopulation()[pos1];
			else if (candidate2Fitness>candidate1Fitness)
					return super.gaInfo.getPopulation()[pos2];
				  else if (Randomness.random.nextFloat()<0.5)
					  		return super.gaInfo.getPopulation()[pos1];
		                else
		                	return super.gaInfo.getPopulation()[pos2];

	}

}
